package com.unknownman.block.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lianziyou.chain.common.api.CommonPage;
import com.lianziyou.chain.common.api.CommonParam;
import com.lianziyou.chain.common.api.CommonResult;
import com.unknownman.block.entity.TChainConfig;
import com.unknownman.block.service.TChainConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.cloud.annotation.SystemResource;
import org.cloud.constant.CoreConstant;
import org.cloud.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "ChainBlockController", tags = "ChainBlockController")
@Validated
@RestController
@RequestMapping("/admin/chain")
@SystemResource(path = "/admin/chain")
@Slf4j
public class ChainBlockController {

    @Autowired
    private TChainConfigService chainConfigService;

    @ApiOperation("获取链配置")
    @SystemResource(value = "/admin-getChainList", description = "获取链配置", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/getChainList")
    public CommonResult<CommonPage<TChainConfig>> getChainList(CommonParam param) throws BusinessException {
        return CommonResult.success(chainConfigService.findListByPage(param.getPage(),param.getLimit(),"create_date desc",new QueryWrapper<>()));
    }

    @ApiOperation("编辑链配置")
    @SystemResource(value = "/admin-updateChain", description = "编辑链配置", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/updateChain")
    public CommonResult updateChain(@RequestBody TChainConfig config) throws BusinessException {
        try {
            return chainConfigService.updateChain(config);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }
    }

    @ApiOperation("添加链配置")
    @SystemResource(value = "/admin-addChain", description = "添加链配置", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/addChain")
    public CommonResult addChain(@RequestBody TChainConfig config) throws BusinessException {
        try {
            return chainConfigService.addChain(config);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }
    }

    @ApiOperation("启用禁用")
    @SystemResource(value = "/admin-updateStatus", description = "启用禁用", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/updateStatus")
    public CommonResult updateStatus(Long chainConfigId,Integer status) throws BusinessException {
        try {
            return chainConfigService.updateStatus(chainConfigId,status);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(e.getMessage());
        }
    }
}
